#!/usr/bin/env bash
echo "begin $0"
source ~/.profile 

output=$1

#get this script real directory
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    # if $SOURCE was a relative symlink, 
    #we need to resolve it relative to the path 
    #where the symlink file was located
    [[ $SOURCE != /*  ]] && SOURCE="$this_script_dir/$SOURCE"
done
this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"

remote_cur_dir=`pwd`
cd $remote_cur_dir
status_dir=`cd $this_script_dir/.. && pwd`

####################################################################
#incude tools script
tool_sh_dir=$HOME/git/tool_sh
remote_external=($tool_sh_dir/remote_compile/builder.sh
                 $tool_sh_dir/remote_compile/git_commit.sh
                 $tool_sh_dir/remote_compile/host_info.sh
                 $tool_sh_dir/set_env.sh
                )

#cd $HOME/git/tool_sh  && git pull >/dev/null
cd $status_dir

for((i=0; i < ${#remote_external[*]}; i++))
do
    if [ -f ${remote_external[$i]} ] ;then
        source ${remote_external[$i]} 
    else
        echo "-- errot,file not exist,which name is:${remote_external[$i]}"
        exit 1
    fi
done


if [ "$output"x == "to_screen"x ] || [ "$output"x == ""x ];then
    echo "-- make output to screen"
elif [ "$output"x == "to_file"x ];then
    #echo "-- make output to file: $make_output_file"
    exec 3>&1
    exec 4>&2

    exec 1>>$make_output_file
    exec 2>>$make_output_file
    printf "\n\n\n\n"
fi

printf "%s currnt make output file is:%s\n" "--" "$make_output_file"

cp_etc_file()
{
    cur_dir_cp_ect_file=`pwd`
    dst_etc_dir=$HOME/etc
    if [ ! -d $dst_etc_dir ]; then
        mkdir -p $dst_etc_dir
    fi

    #if it is not exist, copy etc/$1 which is under current directory
    if [ ! -f "$dst_etc_dir/$1" ]; then
        cp $1 $dst_etc_dir/
    fi
    cd $cur_dir_cp_ect_file
}

#set_zk_connection_env function defined in builder.sh script
#build_project function defined in builder.sh script
#pull_out_code function defined in git_commit.sh script
#pull_out_code_simple function defined in git_commit.sh script

set_zk_connection_env

#############################################################
#build zsmartmq
build_zsmartmq()
{
    build_zsmartmq_cur_dir=`pwd`
    code_dir_home=$HOME/tools/runtime
    zmq_dst_dir=$code_dir_home/zsmartmq
    if [ ! -d $zmq_dst_dir ]; then
        mkdir -p $zmq_dst_dir
        if [ ! -f $code_dir_home/zmq4cpp_aux_cpu.tar.gz ] ;then
            cd  $code_dir_home
            wget -cq $my_source_url/zmq4cpp_aux_cpu.tar.gz
        fi

        if [ -f $code_dir_home/zmq4cpp_aux_cpu.tar.gz ];then
            cd  $code_dir_home&&  tar -zxf zmq4cpp_aux_cpu.tar.gz
        fi
        cd zmq4cpp_aux_cpu 
        ./deploy.sh
        cd `ls -rt | grep install| tail -n 1` && cd zmq-client4cpp && cp -rp ./* $zmq_dst_dir
        cd  $code_dir_home
        cd $build_zsmartmq_cur_dir
        insert_profile "ZSMARTMQ_HOME" "$zmq_dst_dir" 1
    fi
}
#build_zsmartmq


#############################################################
#build fmt
build_fmt()
{
    build_fmt_test_cur_dir=`pwd`
    code_dir_home=$HOME/git/tarball/fmt_v3
    if [ ! -f $code_dir_home/build/fmt/libfmt.so ]; then
        if [ ! -d $HOME/git/tarball/fmt_v3 ]; then
            cd  $HOME/git/tarball
            if [ ! -f fmt_v3.tar.gz ]; then
                wget -cq $my_source_url/fmt_v3.tar.gz
            fi
            tar -zxf fmt_v3.tar.gz
        fi
        cd $build_fmt_test_cur_dir
        build_lib_so $code_dir_home "-DFMT_TEST=OFF -DFMT_DOC=OFF -DFMT_TEST=OFF"
    fi
}
#build_fmt


#############################################################
#build googletest
build_google_test()
{
    build_google_test_cur_dir=`pwd`
    code_dir_home=$HOME/git/tarball/googletest/googletest
    if [ ! -f $code_dir_home/build/libgtest.so ]; then
        if [ ! -d $HOME/git/tarball/googletest ]; then
            cd  $HOME/git/tarball
            if [ ! -f googletest.tar.gz ]; then
                wget -cq $my_source_url/googletest.tar.gz
            fi
            tar -zxf googletest.tar.gz
        fi
        cd $build_google_test_cur_dir
        build_lib_so $code_dir_home
    fi
}
#build_google_test

#############################################################
#build daf
build_daf()
{
    code_dir_home=$HOME/src/daf
    pull_out_code_simple $code_dir_home
    build_project $code_dir_home
    cp -rp $code_dir_home/build/bin/* ~/bin/
    cp -rp $code_dir_home/build/lib/* ~/lib/
}
#build_daf

#############################################################
#compile libcfg.so
#pull out libvtool code from git
pull_out_code_simple $status_dir
build_project $status_dir &&cd $status_dir/build && make install

#build_test $status_dir/cfg/test
#build_project $status_dir/charging_rest
#############################################################
#compile ZSmartAgnet
#pull out ZSmartAgnet code from git
zsmartagent_dir=$status_dir/ZSmartAgent

#rm $HOME/etc/AppInit.json
#cp_etc_file $zsmartagent_dir/etc/AppCluster.json
#cp_etc_file $zsmartagent_dir/etc/AppInit.json
#cp_etc_file $zsmartagent_dir/etc/AppCount.json


#############################################################
#boost test
#build_test $zsmartagent_dir/test

#############################################################
#google test
#build_test $zsmartagent_dir/google_test
run_test()
{
   run_test_cur_dir=`pwd` 
   test_dir=$status_dir/build/test
   cd $test_dir
   for test_d in $(ls | grep test)
   do
       cd $test_dir/$test_d
       runner=`ls| grep test_|grep -v -E "_cfg|_nghttp"`
       if [ -n "$runner" ];then
           echo "-- run $runner"
           ./$runner
       fi
   done
   cd $run_test_cur_dir
   unset run_test_cur_dir
}
#run_test
echo "-------------`date +%Y-%m-%d_%H:%M:%S`---------------"
echo "-------------complie & test end---------------"

if [ "$output"x == "to_file"x ];then
    exec 1>&3
    exec 2>&4
fi

exit 0

